%funcion con la que se va a trabajar 
format long
clc
T=1;
F = inline('((4*exp(-1.5*x)).*(sin(2*pi*x))).^2');
Q = quad(F,0,T/2);
RMC=Q^0.5;
pause

disp('UTILIZANDO LIBRERIAS DE MATHLAB LA INTEGRAL ES:')
disp('...............................................')
disp(RMC)
disp('...............................................')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pause
a=0;
b=0.5;
%fxa=0;
%fxi=0;
n=1;
j=1;
%i=1;
disp('...............................................')
disp('************  TRAPECIO COMPUESTO  *************')
disp('...............................................')

while n<=128
    disp('Segmentos')
    disp(n)
    h=(b-a)/n;
    xi=h;
    i=1;
    fxi=0;
    fxa=0;
    
        while i<n
            %disp(fxa);
            fxa=2*(4*exp(-1.5*xi))*(sin(2*pi*xi));
            %disp(xi)
            fxi=fxi+fxa;
            xi=xi+h;
            i=i+1;
        end
        
    fxo=(4*exp(-1.5*a)).*(sin(2*pi*a));
    fxn=(4*exp(-1.5*b)).*(sin(2*pi*b));
    I=(h/2)*(fxo+fxi+fxn);
    RMC1=I^0.5;
    n=2^j;
    j=j+1;
    errorvr=abs((RMC-RMC1)./RMC)*100;
    fpa=(-6*exp(-1.5*a))*(sin(2*pi*a))+8*pi*exp(-1.5*a)*(cos(2*pi*a));
    fpb=(-6*exp(-1.5*b))*(sin(2*pi*b))+8*pi*exp(-1.5*b)*(cos(2*pi*b));
    errora=(-1/12)*((b-a)^3)*(fpb-fpa)/(b-a);
    errorvd=RMC-RMC1;
    datos1=struct('VALOR_TEORICO',RMC,'VALOR_CALCULADO',RMC1,'ERROR_VERDADERO_RELATIVO',errorv,'ERROR_ABSOLUTO',errora,'ERROR_VERDADERO',errorvd);
    disp(datos1)
end
disp('...............................................')    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Cuadratura de Gauss con 5 puntos

a0=(a+b)./2;
a1=(b-a)./2;
%factores
c0=0.2369269;
c1=0.4786287;
c2=0.5688889;
c3=0.4786287;
c4=0.2369269;
%argumentos
x0=-0.906179846;
x1=-0.538469310;
x2=0;
x3=0.538469310;
x4=0.906179846;
IC=c0*a1*((4*exp(-1.5*(a0+a1*x0))).*(sin(2*pi*(a0+a1*x0))))+c1*a1*((4*exp(-1.5*(a0+a1*x1))).*(sin(2*pi*(a0+a1*x1))))+c2*a1*((4*exp(-1.5*(a0+a1*x2))).*(sin(2*pi*(a0+a1*x2))))+c3*a1*((4*exp(-1.5*(a0+a1*x3))).*(sin(2*pi*(a0+a1*x3))))+c4*a1*((4*exp(-1.5*(a0+a1*x4))).*(sin(2*pi*(a0+a1*x4))));
RMC2=IC^0.5;
error=((abs(RMC-RMC2))/RMC)*100;
error
disp('...............................................')
disp('************  CUADRATURA DE GAUSS  ************')
disp('...............................................')
datos2=struct('VALOR_TEORICO',RMC,'VALOR_CALCULADO',RMC2,'ERROR_VERDADERO',error);
disp(datos2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Simpson compuesto
disp('...............................................')
disp('*************  SIMPSON COMPUESTO  *************')
disp('...............................................')

n = 2;
es= 0;
t=1;

while n < 128

h=T/(2*n);

i=1;
x=0;
while i<=(n+1)
    
     Fun(i)   =(4*exp(-1.5*x)).*(sin(2*pi*x));
     x=x + h;
     i=i + 1;
 end
 
 i=5;
 Imp= Fun(3);
 while i<=(n)
     Imp = Imp +Fun(i);
     i=i+2;    
 end
 
 i=4;
 Par=Fun(2);
 while i<=(n)
     Par = Par +Fun(i);
     i=i+2;    
 end
 
 I(t)=(T/2) * ( Fun(1) + 4*Imp +2*Par + Fun(n+1)) / (3*n);
 rmc(t)=I(t)^0.5;

es= es+1;
n= n + 2^es;
t=t+1;
end
f=t-1;
t=1;

while t <=  f 
    Ev(t) = 100*(abs(RMC-rmc(t))/RMC);
    datos(t)=struct('VALOR_TEORICO',RMC,'VALOR_CALCULADO',rmc(t),'ERROR_VERDADERO',Ev(t));
    disp(datos(t));
    %pause
    t= t + 1;
    
end    
pause



    



